package cn.cloudreal.cbms.statements.dao.impl;

import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;

import cn.cloudreal.cbms.common.util.PageBean;
import cn.cloudreal.cbms.statements.dao.ProjectPriceDao;
import cn.cloudreal.cbms.vo.ProjectPrice;

/**
 * <p>
 * Title: ITC项目费用预算的获取数据
 * </p>
 * <p>
 * Copyright: Copyright (c) 2012
 * </p>
 * <p>
 * Company:云恒瑞通
 * </p>
 * 
 * @author xiayundong
 * @date 2012-8-14
 */
public class ProjectPriceDaoImpl extends SqlMapClientDaoSupport implements ProjectPriceDao{

	private final String GET_PROJECTPRICE_LIST = "projectprice.getProjectPriceList";
	private final String GET_PROJECTPRICE_COUNT = "projectprice.getProjectPriceCount";
	/** 日志工具类实例 */
    private static final Logger log = Logger.getLogger(ProjectPriceDaoImpl.class);
    
	/**
	 * 获取项目费用结算列表的总的数量
	 * @param condition
	 * @return
	 */
	public List<ProjectPrice> getProjectPriceList(Map condition, PageBean page) {
		// 打印进入方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Enter ProjectPriceDaoImpl getProjectPriceList() method, return List<ProjectPrice>");
        }
        
		List<ProjectPrice> projectPriceList = null;
        int totalCount = 0;
        totalCount = getProjectPriceCount(condition);
        page.setTotalRecords(totalCount);
		// 总记录数不为零才查询
        if (totalCount > 0)
        {
        	condition.put("i_type", 2);
    		condition.put("i_startid", (page.getPageNo()-1) * page.getPageSize()+1);
    		condition.put("i_endid", (page.getPageNo()-1) * page.getPageSize() + page.getPageSize());
        	// 查询
    		projectPriceList = getSqlMapClientTemplate().queryForList(GET_PROJECTPRICE_LIST, condition);
    		if(totalCount<11)
    		{
    			for(int i=0;i<projectPriceList.size();i++)
    			{
    				String projectName = projectPriceList.get(i).getProjectName();
    				if(projectName.equals("本页合计"))
    				{
    					projectPriceList.remove(i);
    				}
    			}
    		}
        }
        // 打印退出方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Exit ProjectPriceDaoImpl getProjectPriceList() method, return List<ProjectPrice>");
        }
        return projectPriceList;
	}
	
	/**
	 * 获取项目费用结算的列表
	 * @param condition
	 * @param page
	 * @return
	 */
	public int getProjectPriceCount(Map condition) {
		Object count = 0;
		try 
		{
			condition.put("i_type", 1);
			count = getSqlMapClientTemplate().queryForObject(GET_PROJECTPRICE_COUNT, condition);
			System.out.println("count========>"+count);
		} catch (Exception e) {
		     e.printStackTrace();
		}
		return count == null ? 0 : (Integer)count;
	
	}
	
	/**
	 * 获取所有项目费用结算的列表（无分页）
	 * @param condition
	 * @return
	 */
	public List<ProjectPrice> getTotalProjectPriceList(Map condition) {
		// 打印进入方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Enter ProjectPriceDaoImpl getTotalProjectPriceList() method, return List<ProjectPrice>");
        }
        List<ProjectPrice> projectPriceList = null;
        try {
        	int count = getProjectPriceCount(condition);
        	if (count >0)
        	{
        		condition.put("i_type", 2);
        		condition.put("i_startid", 1);
        		condition.put("i_endid", count);
        		// 查询
        		projectPriceList = getSqlMapClientTemplate().queryForList(GET_PROJECTPRICE_LIST, condition);
        	}
        	
        	
        } 
        catch (Exception e) 
        {
        	e.printStackTrace();
        }
        // 打印退出方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Exit ProjectPriceDaoImpl getTotalProjectPriceList() method, return List<ProjectPrice>");
        }
		
		return projectPriceList;
	}

}
